package org.llw.ldailytask.dao;

import java.util.ArrayList;
import java.util.List;

import org.llw.ldailytask.entity.Task;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqliteDao extends SQLiteOpenHelper {

	private SQLiteDatabase dbwrite;
	private SQLiteDatabase dbread;
	private ContentValues cv;

	public SqliteDao(Context context) {
		super(context, "DailyTaskDB.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE if not exists task([_id] INTEGER PRIMARY KEY AUTOINCREMENT,[title] varchar(100),[content] varchar(250),[type] varchar(20),[status] varchar(20),[date] varchar(12),[time] timestamp(10),[isalert] varchar(20),[alert_type] varchar(20));");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public void addTask(String date, String time, String title, String content,
			String type, String status, String isAlert) {
		dbwrite = getWritableDatabase();
		dbwrite.execSQL(
				"INSERT INTO task (title,content,type,status,date,time,isalert,alert_type) values(?,?,?,?,?,?,?,?)",
				new Object[] { title, content, type, status, date,
						timeFormat(time), isAlert, 0 });
		dbwrite.close();
	}

	public void editTask(Integer id, String date, String time, String title,
			String content, String type, String status, String isAlert) {
		dbwrite = getWritableDatabase();
		dbwrite.execSQL(
				"UPDATE task SET title=?,content=?,type=?,status=?,date=?,time=?,isalert=?,alert_type=? WHERE _id=?",
				new Object[] { title, content, type, status, date,
						timeFormat(time), isAlert, 0, id });
		dbwrite.close();
	}

	public void delTask(int id) {
		dbwrite = getWritableDatabase();
		dbwrite.execSQL("DELETE FROM task where _id = " + id);
		dbwrite.close();
	}

	public List<Task> getDailyTasks(String date) {
		List<Task> taskList = new ArrayList<Task>();
		dbread = getReadableDatabase();
		// '2015-1-1 00:00:00' '2015-1-1 23:59:59'
		Cursor cursor = dbread
				.rawQuery(
						"select * from task where time >= ? and time <= ? order by time",
						new String[] { timeFormat2(date) + " 00:00:00.000",
								timeFormat2(date) + " 23:59:59.000" });
		while (cursor.moveToNext()) {
			Task t = new Task();
			t.setId(cursor.getInt(0));
			t.setTitle(cursor.getString(1));
			t.setContent(cursor.getString(2));
			t.setType(cursor.getString(3));
			t.setStatus(cursor.getString(4));
			t.setDate(cursor.getString(5));
			t.setTime(cursor.getString(6));
			t.setIsAlert(cursor.getString(7));
			taskList.add(t);
		}
		cursor.close();
		dbread.close();
		return taskList;
	}

	private String timeFormat(String time) {
		// 传进来这种格式 2015-4-1 04:37:00
		// 时间戳格式 必须是 yyyy-MM-dd HH:mm:ss.sss
		time = time + ".000";
		String str[] = time.split("-");
		if (str[1].length() == 1) {
			str[1] = "0" + str[1];
		}
		if (str[2].length() == 14) {
			str[2] = "0" + str[2];
		}
		return str[0] + "-" + str[1] + "-" + str[2];
	}

	private String timeFormat2(String time) {
		String str[] = time.split("-");
		if (str[1].length() == 1) {
			str[1] = "0" + str[1];
		}
		if (str[2].length() == 1) {
			str[2] = "0" + str[2];
		}
		return str[0] + "-" + str[1] + "-" + str[2];
	}

}
